﻿

namespace Cid.Models
{
	using System;
	using System.Collections.Generic;
	using System.IO;
	using System.Linq;
	using System.Text;

	using MbUnit.Framework;

	using Cid.Data;

	[TestFixture]
	public class Spec
	{

		public static Adapter Adapter { get; set; }
		public static string ConnectionString { get; set; }

		internal IEnumerable<string> DbFixtures { get; set; }
		public static Fixtures Runner { get; set; }
		public bool RunFixturesOnEveryTest { get; set; }


		public Spec()
		{
			this.GetFixtures();
			this.RunFixturesOnEveryTest = true;
		}

		[FixtureSetUp]
		public virtual void InvokeOnceBeforeTests()
		{
			
		}

		protected void GetFixtures()
		{
			if (Runner == null)
				Runner = Fixtures.New(Directory.GetCurrentDirectory() + "\\Fixtures",
					Adapter);
			
			Type type = this.GetType();
			object[] items = type.GetCustomAttributes(typeof(FixtureAttribute), true);
			
			List<string> fixtures = new List<string>();
			foreach (FixtureAttribute item in items)
				fixtures.Add(item.Name);

			this.DbFixtures = fixtures;

			if (this.DbFixtures != null)
				this.DbFixtures.Each(fixture => Runner[fixture].RenewFixtures());
		}


		[SetUp]
		public virtual void InvokeBeforeEveryTest()
		{
			if (this.DbFixtures != null && this.RunFixturesOnEveryTest)
				this.DbFixtures.Each(fixture => Runner[fixture].RenewFixtures());
		}

		[TearDown]
		public virtual void InvokeAfterEveryTest()
		{

		}

		[FixtureTearDown]
		public virtual void InvokeOnceAfterTests()
		{

		}
	}
}
